from __future__ import with_statement
import sys, os
from twisted.internet import reactor, defer
import ConnectionTable

connection_table = ConnectionTable.ConnectionTable()

def add_connection(transport):
    connection_table.add_to_group(transport)

def remove_connection(transport):
    connection_table.remove(transport)

def receive_line(transport, line):
    clean_line = line.strip()
    connection_table.receive_line(transport, clean_line)

def drop_connection( conn ):
    del conn.user.connection
    conn.transport.loseConnection()
    connection_table.remove(self)

def is_logged_in( username ):
    return connection_table.is_logged_in( username )

def to_all( message, from_trans=None ):
    connection_table.broadcast( message, from_trans )

def get_playing():
    return [conn.user for conn in connection_table.get_playing()]

def get_character_in_world(name):
    """Returns the first player character with `name`"""
    for conn in connection_table.get_playing():
        if conn.user.username.lower() == name.strip().lower():
            return conn.user
    return None
